package org.graduation.project.controller;

import org.graduation.common.NoPermissionException;
import org.graduation.feignapi.RBACClient;
import org.graduation.feignapi.UserClient;
import org.graduation.model.common.RBAC.Permissions;
import org.graduation.model.common.RBAC.Roles;
import org.graduation.model.common.ResponseResult;
import org.graduation.model.common.project.Project;
import org.graduation.model.common.project.ProjectUserTable;
import org.graduation.model.common.user.User;
import org.graduation.model.dtos.RBAC.AssignAndRevokeRoleDTO;
import org.graduation.model.dtos.RBAC.CheckPermissionsDTO;
import org.graduation.project.service.PhaseService;
import org.graduation.project.service.ProjectAndUserService;
import org.graduation.project.service.ProjectService;
import org.graduation.utils.Token;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.LinkedHashMap;
import java.util.Map;

@RestController
@RequestMapping("/project")
public class ProjectController {
    @Autowired
    private ProjectService projectService;
    @Autowired
    private PhaseService phaseService;
    @Autowired
    private ProjectAndUserService projectAndUserService;
    @Autowired
    private Token token;
    @Autowired
    private UserClient userClient;
    @Autowired
    private RBACClient rbacClient;

    @PostMapping("/create")
    public ResponseResult createProject(@RequestBody Project project, HttpServletRequest request) {
        Integer uid = token.getUidByToken(token.getTokenByRequest(request));
        User owner = new User();
        owner.setUid(uid);
        project.setOwner(uid);
        if (projectService.save(project)) {
            rbacClient.assignRole(new AssignAndRevokeRoleDTO(project.getPId(), uid, Roles.CREATOR.getRoleId()));
            projectAndUserService.join(new ProjectUserTable(project, owner));
            //init phase
            phaseService.initialPhase(project);
            return ResponseResult.success();
        } else {
            return ResponseResult.failed("创建项目失败,请稍候再试");
        }
    }

    @GetMapping("/list/{page}")
    public ResponseResult listByUser(@PathVariable Integer page, HttpServletRequest httpServletRequest) {
        Integer uid = token.getUidByToken(token.getTokenByRequest(httpServletRequest));
        return ResponseResult.success(projectService.listByUser(uid, page));
    }

    @PostMapping("/{p_id}/invite/{email}")
    public ResponseResult invite(@PathVariable Long p_id, @PathVariable String email) {
        Project project = projectService.getById(p_id);
        User user = userClient.info(email).getData();
        if (user == null) {
            return ResponseResult.failed("该用户不存在");
        }
        return projectService.invite(project, user);
    }

    @PostMapping("/{p_id}/remove/{u_id}")
    public ResponseResult removeUser(@PathVariable int p_id, @PathVariable int u_id) {
        Project project = projectService.getById(p_id);
        User user = (User) userClient.getInfoById(u_id).getData();
        return projectService.removeUser(project, user);
    }

    @GetMapping("/info/{p_id}")
    public ResponseResult info(@PathVariable long p_id, HttpServletRequest httpServletRequest) {
        Permissions permission = Permissions.VIEW_PROJECT;
        int uid = token.getUidByToken(token.getTokenByRequest(httpServletRequest));
        CheckPermissionsDTO checkPermissionsDTO = new CheckPermissionsDTO(p_id, uid, permission);
        if (rbacClient.check(checkPermissionsDTO).getCode() == 200) {
            return ResponseResult.success(projectService.getById(p_id));
        } else {
            throw new NoPermissionException(permission);
        }
    }
}
